﻿namespace AnyOffice.general.netdisk
{
    using log4net;
    using System;
    using System.Data;
    using System.IO;
    using System.Reflection;

    public class DirectoryUtility
    {
        private DataTable _dtbInfo = new DataTable("CurrentFolder");
        private int _fileCount;
        private long _totalSize;
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private object[] O1lll = new object[] { "index", typeof(int), "path", typeof(string), "name", typeof(string), "isDirectory", typeof(bool), "size", typeof(int), "creationTime", typeof(DateTime), "img", typeof(string) };

        public DirectoryUtility(string relativePath)
        {
            for (int i = 0; i < this.O1lll.Length / 2; i++)
            {
                this._dtbInfo.Columns.Add(new DataColumn((string) this.O1lll[i * 2], (Type) this.O1lll[i * 2 + 1]));
            }
            this._dtbInfo.Columns[0].AutoIncrement = true;
            this._dtbInfo.Columns[0].Unique = true;
            NetdiskInfo netdiskInfo = NetdiskFactory.GetNetdiskInfo();
            if (netdiskInfo != null)
            {
                string diskPath = netdiskInfo.DiskPath;
                if (relativePath != "/") diskPath = diskPath + relativePath.Replace("../", "");
                diskPath = diskPath.Replace("/", @"\");
                try
                {
                    if (Directory.Exists(diskPath))
                    {
                        DataRow row;
                        string str2 = relativePath;
                        if (str2 == "/") str2 = "";
                        if (str2.EndsWith("/")) str2.Substring(0, str2.Length - 1);
                        str2 = str2.Replace("../", "");
                        string[] directories = Directory.GetDirectories(diskPath);
                        for (int j = 0; j < directories.Length; j++)
                        {
                            DirectoryInfo info2 = new DirectoryInfo(directories[j]);
                            row = this._dtbInfo.NewRow();
                            row[1] = str2 + "/" + info2.Name;
                            row[2] = info2.Name;
                            row[3] = true;
                            row[4] = 0;
                            row[5] = info2.LastWriteTime;
                            row[6] = "folder.gif";
                            this._dtbInfo.Rows.Add(row);
                        }
                        string str3 = ".docx.xlsx.pptx.ace.app.arj.asp.av.gif.avi.bmp.cab.chm.com.css.dll.doc.down.exe.gif.htm.html.img.jpeg.jpg.js.m3u.mdb.mid.mov.mp3.mpeg.mpg.pdf.ppt.ra.ram.rar.rm.sql.swf.tar.txt.url.wav.wmv.xls.zip.";
                        string[] files = Directory.GetFiles(diskPath);
                        for (int k = 0; k < files.Length; k++)
                        {
                            FileInfo info3 = new FileInfo(files[k]);
                            row = this._dtbInfo.NewRow();
                            row[1] = str2 + "/" + info3.Name;
                            row[2] = info3.Name;
                            row[3] = false;
                            row[4] = info3.Length;
                            row[5] = info3.LastWriteTime;
                            if (str3.IndexOf(info3.Extension.ToLower() + ".") >= 0 && info3.Extension != string.Empty)
                                row[6] = info3.Extension.Replace(".", "") + ".gif";
                            else
                                row[6] = "unknown.gif";
                            this._dtbInfo.Rows.Add(row);
                            this._totalSize += info3.Length;
                            this._fileCount++;
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled) log.Error("", exception);
                }
            }
        }

        public DataTable DtbInfo
        {
            get
            {
                return this._dtbInfo;
            }
        }

        public int FileCount
        {
            get
            {
                return this._fileCount;
            }
        }

        public long TotalSize
        {
            get
            {
                return this._totalSize;
            }
        }
    }
}

